iT邦幫忙

2024 iThome 鐵人賽

DAY 10
0

今天是第十天我想要跑一個Lstm模型訓練,以下是程式碼跟資料集

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout, BatchNormalization,Conv1D, MaxPooling1D
from tensorflow.keras.callbacks import ModelCheckpoint,ReduceLROnPlateau,EarlyStopping
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import Huber
from tensorflow.keras.regularizers import l1,l2
from sklearn.preprocessing import MinMaxScaler
import numpy as np
import torch
import pandas as pd
import matplotlib.pyplot as plt


def create_dataset(dataset,size):
    question,ans = [],[]
    for i in range(len(dataset) - size):
        q_array = dataset[i:i + size]
        a_array = dataset[i + size]
        ans.append(a_array)
        question.append(q_array)
    return np.array(question), np.array(ans)
lookback = 32

model = Sequential([
                    LSTM(200, activation = 'relu', return_sequences=True,input_shape=(lookback, 2),kernel_regularizer=l1(0.1)),
                    Dropout(0.1),
                    LSTM(200, activation='relu', return_sequences=False,kernel_regularizer=l2(0.1)),
                    Dense(2, kernel_regularizer=l1(0.1))])

model.compile(optimizer=Adam(learning_rate=0.0002), loss=Huber())
input_data = pd.read_csv("1fish_11000.csv")
input_data = input_data[["x","y"]].values.astype('float32')
train,test = create_dataset(input_data,lookback)
print(train.shape)
print(test.shape)
early_stopping = EarlyStopping(monitor='val_loss', patience=50, restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, min_lr=1e-6)
checkpoint = ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True, mode='min')
history = model.fit(train, test, epochs=6000,  batch_size=lookback, callbacks=[checkpoint, early_stopping, reduce_lr])
plt.plot(history.history['loss'], label='Loss',c = "g")
plt.title('Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(history.history['accuracy'], label='accuracy',c = "b")
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy.')
plt.legend()
plt.show()
model.save('new lstm.h5')

這段程式碼建立並訓練了一個基於LSTM(Long Short-Term Memory)層的神經網絡模型,來預測某個時間序列資料的未來值。以下是程式碼的詳細解釋:

1. 引入必要的函式庫

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import Huber
from tensorflow.keras.regularizers import l1, l2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

這段程式碼匯入了建構與訓練深度學習模型所需的模組。這些模組包括:

  • Keras 模組:用來建立深度學習模型。
  • NumPyPandas:用來處理數據。
  • Matplotlib:用來視覺化訓練過程中的損失和準確率。

2. 定義數據集的處理函式

def create_dataset(dataset, size):
    question, ans = [], []
    for i in range(len(dataset) - size):
        q_array = dataset[i:i + size]
        a_array = dataset[i + size]
        ans.append(a_array)
        question.append(q_array)
    return np.array(question), np.array(ans)

這個函式將數據集轉換成LSTM模型可接受的格式。具體來說,它會創建一系列的輸入(question)和對應的標籤(ans),用來預測下一步的數值。

3. 設定模型的架構

model = Sequential([
    LSTM(200, activation='relu', return_sequences=True, input_shape=(lookback, 2), kernel_regularizer=l1(0.1)),
    Dropout(0.1),
    LSTM(200, activation='relu', return_sequences=False, kernel_regularizer=l2(0.1)),
    Dense(2, kernel_regularizer=l1(0.1))
])

這個模型包含兩個LSTM層和一個全連接層(Dense layer)。具體來說:

  • 第一層LSTM:有200個單元,使用ReLU激活函數,並返回完整序列。
  • Dropout層:以0.1的比例隨機丟棄一些神經元,防止過擬合。
  • 第二層LSTM:同樣有200個單元,但不返回序列,只返回最後的輸出。
  • 全連接層:輸出大小為2,對應xy

此外,這些層都使用了L1和L2正則化來控制模型的複雜度,防止過擬合。

4. 編譯模型

model.compile(optimizer=Adam(learning_rate=0.0002), loss=Huber())

這段程式碼編譯模型,設定使用Adam優化器和Huber損失函數。

5. 資料準備

input_data = pd.read_csv("1fish_11000.csv")
input_data = input_data[["x","y"]].values.astype('float32')
train, test = create_dataset(input_data, lookback)
print(train.shape)
print(test.shape)

這裡從CSV檔案讀取數據,並使用前面定義的create_dataset函式來準備訓練和測試數據。lookback是序列的長度,設定為32。

6. 訓練過程中的回調函數

early_stopping = EarlyStopping(monitor='val_loss', patience=50, restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, min_lr=1e-6)
checkpoint = ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True, mode='min')

這裡定義了三個回調函數:

  • EarlyStopping:監控val_loss,如果50個epoch內沒有改善,則停止訓練,並恢復至最佳模型權重。
  • ReduceLROnPlateau:當val_loss停止改善時,將學習率降低一半。
  • ModelCheckpoint:在每個epoch後保存最佳模型。

7. 訓練模型

history = model.fit(train, test, epochs=6000, batch_size=lookback, callbacks=[checkpoint, early_stopping, reduce_lr])

這段程式碼以指定的epochs(6000)和batch_size(32)訓練模型,並使用前述的回調函數來控制訓練過程。

8. 視覺化訓練過程

plt.plot(history.history['loss'], label='Loss', c="g")
plt.title('Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

plt.plot(history.history['accuracy'], label='Accuracy', c="b")
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()

這部分代碼可視化了模型訓練過程中的損失(Loss)和準確率(Accuracy)。不過,這裡的history.history裡並沒有準確率的資料,所以在這裡的Accuracy可視化會報錯。模型的損失則會成功顯示。

9. 保存模型

model.save('new lstm.h5')

最後,將訓練好的模型保存為H5格式的檔案,以便將來載入和使用。
因為訓練要跑6000次訓練可能要跑快一天,注意不要關掉程式就好。


上一篇
Day 9 Lstm候鳥行為預測
下一篇
day Lstm模型訓練完成成果圖
系列文
LSTM結合Yolo v8對於多隻斑馬魚行為分析29
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言